//
// Created by win on 2017/2/20.
//


#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/thrift/config.h"
#include "../../../src/module/thrift/tcompact/tcompact.h"
#include "../../../src/module/thrift/thrift_core.h"


#include "apmtest_cunit.h"
#include "apmtest_comm.h"
static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}




static void case1(void)
{
    unsigned char *p_tcp_body = NULL;
    unsigned int ui_tcp_body_len = 7;

    int i_ret = 0;
    p_tcp_body = (unsigned char *)frmemdup("\x00\x00\x00\x03\x82\x41\x00", 7);
    i_ret = frthrift_tcompact_direction_detect(p_tcp_body, ui_tcp_body_len);

    CU_ASSERT_EQUAL(i_ret, FR_DIRECT_SRV2CLIENT);
    frfree(p_tcp_body);

    return;
}


static void case2(void)
{
    unsigned char *p_tcp_body = NULL;
    unsigned int ui_tcp_body_len = 7;

    int i_ret = 0;
    p_tcp_body = (unsigned char *)frmemdup("\x00\x00\x00\x02\x82\x21\x00", 7);

    i_ret = frthrift_tcompact_direction_detect(p_tcp_body, ui_tcp_body_len);

    CU_ASSERT_EQUAL(i_ret, FR_DIRECT_CLIENT2SRV);
    frfree(p_tcp_body);

    return;
}

static void case3(void)
{
    unsigned char *p_tcp_body = NULL;
    unsigned int ui_tcp_body_len = 7;

    int i_ret = 0;
    p_tcp_body = (unsigned char *)frmemdup("\x00\x00\x00\x03\x82\xf1\x00", 7);

    i_ret = frthrift_tcompact_direction_detect(p_tcp_body, ui_tcp_body_len);

    CU_ASSERT_EQUAL(i_ret, FR_DIRECT_UNKNOWN);
    frfree(p_tcp_body);

    return;
}


static void case4(void)
{
    unsigned char *p_tcp_body = NULL;
    unsigned int ui_tcp_body_len = 0;

    int i_ret = 0;
    i_ret = frthrift_tcompact_direction_detect(p_tcp_body, ui_tcp_body_len);

    CU_ASSERT_EQUAL(i_ret, FR_DIRECT_UNKNOWN);

    return;
}

int add_suite_frthrift_tcompact_direction_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" frthrift_tcompact_direction_detect()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);


    return FR_SUCC;

}
